﻿using BCData.Common.IP;
using BCDto.Common.IP;
using BCEntity.Common.IP;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.Common.IP
{
    public class IPAddressRecordService : IIPAddressRecordService
    {
        private IIPAddressRecordData ipAddressRecordData;

        public IPAddressRecordService(IIPAddressRecordData ipAddressRecordData)
        {
            this.ipAddressRecordData = ipAddressRecordData;
        }

        public IPAddressRecordDto Get(string ip)
        {
            IPAddressRecordEntity iPAddressRecordEntity = new IPAddressRecordDto() { StartIP = ip }.As<IPAddressRecordEntity>();
            return ipAddressRecordData.Get(iPAddressRecordEntity).As<IPAddressRecordDto>();
        }

        public void Import(List<IPAddressRecordDto> ipAddressRecordsDto)
        {
            ipAddressRecordsDto.ForEach(item =>
            {
                IPAddressRecordEntity ipAddressRecordEntity = item.As<IPAddressRecordEntity>();
                if (ipAddressRecordData.CheckIPAddressRecordExists(ipAddressRecordEntity))
                {
                    ipAddressRecordData.Update(ipAddressRecordEntity);
                }
                else
                {
                    ipAddressRecordData.Add(ipAddressRecordEntity);
                }
            });
        }
    }
}
